package com.schoolvisitors.config;

import com.schoolvisitors.repository.LoginTokenRepository;
import com.schoolvisitors.response.ContextInterceptor;
import com.schoolvisitors.service.AdminService;
import com.schoolvisitors.service.UserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final UserService userService;
    private final LoginTokenRepository loginTokenRepository;
    private final AdminService adminService;

    public WebConfig(UserService userService, LoginTokenRepository loginTokenRepository, AdminService adminService) {
        this.userService = userService;
        this.loginTokenRepository = loginTokenRepository;
        this.adminService = adminService;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 ContextInterceptor
        registry.addInterceptor(new ContextInterceptor(userService, adminService, loginTokenRepository))
                .addPathPatterns("/**");  // 拦截所有请求，或根据需要指定路径
    }
}